/*
 * @Author: wcx 1055127249@qq.com
 * @Date: 2025-01-15 17:17:43
 * @LastEditors: wcx 1055127249@qq.com
 * @LastEditTime: 2025-01-20 16:42:35
 * @FilePath: \my-project\src\utils\formItem.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const formItems = [
  { label: '输入框', value: 'input', placeholder: '请输入', is: 'el-input' },
  { label: '数字框', value: 'number', placeholder: '请输入', is: 'el-input-number' },
  { label: '日期选择', value: 'date', placeholder: '请选择', is: 'el-input' },
  { label: '选择器', value: 'select', placeholder: '请选择', is: 'el-select' },
  { label: '单选框', value: 'radio', is: 'el-radio-group' },
  { label: '多选框', value: 'checkbox', is: 'el-checkbox-group' },
  { label: '开关', value: 'switch', is: 'el-switch' },
  { label: '级联选择器', value: 'cascader', placeholder: '请选择', is: 'el-cascader' },
  { label: '时间选择器', value: 'timePicker', placeholder: '请选择', is: 'el-time-select' },
  { label: '日期选择器', value: 'datePicker', placeholder: '请选择', is: 'el-date-picker' },
  { label: '日期时间选择器', value: 'dateTimePicker', placeholder: '请选择', is: 'el-date-picker' },
  { label: '评分', value: 'rate', is: 'el-rate' },
  { label: '颜色选择器', value: 'colorPicker', is: 'el-color-picker' },
  { label: '穿梭框', value: 'transfer', is: 'el-transfer' },
  { label: '多行输入框', value: 'textarea', is: 'el-input', props: { type: 'textarea', rows: 5 } },
]

export function createEleProps(item) {
  const elItem = formItems.find(i => i.value === item.type)
  const placeholder = elItem.placeholder ? `${elItem.placeholder}${item.label}` : ''
  const result = { ...elItem, placeholder, ...item.props }
  if (!elItem) {
    result.is = item.type
  }
  if (item.type === 'cascader') {
    result.options = item.options
  }
  delete result.value
  return result
}
export function createValidator(validator) {
  return async (rule, value, callback) => {
    let result
    if (typeof validator === 'function') {
      result = validator(value, this.form)
    } else {
      result = await new Function('value', 'form', validator)(value, this.form)
    }
    if (typeof result === 'boolean' && result) {
      callback()
    } else {
      callback(result || '输入不合法')
    }
  }
}
export default formItems